//
//  HeapListViewController.m
//  ChongDingTest
//
//  Created by luyee on 2020/9/2.
//  Copyright © 2020 luyee. All rights reserved.
//

#import "HeapListViewController.h"
#import "HeapListViewService.h"
#import "SelectPicScrollView.h"

#import <Masonry.h>

@interface HeapListViewController () <SelectPicScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) HeapListViewService *service;

@property (nonatomic, strong) SelectPicScrollView *heapList;

@property (nonatomic, strong) UITextField *tf;

@end

@implementation HeapListViewController{
    UILabel *label;
}

- (HeapListViewService *)service {
    if (!_service) {
        _service = [[HeapListViewService alloc] init];
        _service.viewCtrl = self;
    }
    return _service;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tf = [[UITextField alloc] init];
    [self.view addSubview:self.tf];
    self.tf.delegate = self;
    self.tf.text = @"汉堡";
    self.tf.placeholder = @"没有相关信息，请重新搜索";
    self.tf.returnKeyType = UIReturnKeySearch;
    
    [self.tf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_offset(20);
        make.right.equalTo(self.view).mas_offset(-20);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).mas_offset(20);
        } else {
            make.top.equalTo(self.view).mas_offset(40);
        }
    }];
    self.service.content = self.tf.text;
    
    self.heapList = [[SelectPicScrollView alloc] initWithFrame:CGRectZero];
    self.heapList.delegate = self;
    [self.view addSubview:self.heapList];
    
    [self.heapList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tf.mas_bottom).mas_offset(20);
        make.bottom.left.right.equalTo(self.view);
    }];
    
    label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor orangeColor];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(-60);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    label.layer.cornerRadius = 25;
    label.clipsToBounds = YES;
    
}

- (void)refresh{
    [self.view bringSubviewToFront:label];
    if (self.service.pageNumber == 1) {
        label.text = @"1";
        [self.heapList refresh:self.service.dataSource];
    } else {
        [self.heapList reload:self.service.dataSource];
    }
}

-(void)leftMoveEndWithIndex:(NSInteger)index{
    label.text = [NSNumber numberWithInteger:index + 2].stringValue;
    NSLog(@"向左移除-------------第index:%ld张",(long)index);
    [self.service.dataSource removeObjectAtIndex:0];
    [self.service loadMore];
}


-(void)rightMoveEndWithIndex:(NSInteger)index{
    label.text = [NSNumber numberWithInteger:index + 2].stringValue;
    NSLog(@"向右移除-------------第index:%ld张",(long)index);
    [self.service.dataSource removeObjectAtIndex:0];
    [self.service loadMore];
}

- (void)touched {
    [self.tf resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    self.service.content = textField.text;
    return [self.tf resignFirstResponder];
}

@end
